<?php
vendor('Utils.Verify');
class MemberAction extends Action
{
	public function index()
	{
		$this->isLogin();
		
		$this->display();
	}
	
	/**
	 * 会员是否已登录
	 */
	private function isLogin()
	{
		$loginFlag = session('member_name');
		if(empty($loginFlag)){
			$this->display('login');
			exit;
		}
		
		$loginInfo['member_name'] = session('member_name');
		$loginInfo['login_date'] = session('login_date');
		$loginInfo['login_ip'] = session('login_ip');
		$loginInfo['member_type'] = session('member_type');
		$loginInfo['member_avatar'] = session('member_avatar');
		$this->assign('loginInfo',$loginInfo);
	}
	
	/**
	 * 用户名是否存在
	 * @param unknown_type $name
	 */
	public function isExistName($name){
		$memberMO = M('Member');
		$rel = $memberMO->where("member_name='$name'")->select();
		return (count($rel) > 0);
	}
	
	/**
	 * email是否存在
	 * @param unknown_type $email
	 */
	public function isExistEmail($email){
		$memberMO = M('Member');
		$rel = $memberMO->where("email='$email'")->select();
		return (count($rel) > 0);
	}
	/**
	 * 模版头
	 */
	public function header()
	{
		$this->display();
	}
	/**
	 * 左边栏
	 */
	public function left()
	{
		$this->display();
	}
	
	/**
	 * 登录信息
	 */
	public function login_info()
	{
		$this->display();
	}
	/**
	 * 登录
	 */
	public function login(){
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			$data['member_name'] = strtolower(trim($_POST['username']));
			$data['member_pwd'] = htmlspecialchars(trim($_POST['pwd']));
			$memberMO = M('Member');
			$rel = $memberMO->where($data)->select();
			if(count($rel) < 1){
				exit('账户或者密码错误！请重新输入');
			}else{
				if($rel[0]['status'] != 1){
					exit('你的账号处理冻结状态，请联系管理员。');
				}
			
				$id = $rel[0]['id'];
				$typeId = $rel[0]['member_type'];
				
				$typeMO = M('MemberType');
				$memberType = $typeMO->where("id=$typeId")->select();
				
				session('member_id',$id);
				session('member_name',$rel[0]['member_name']);
				session('login_date',$rel[0]['login_date']);
				session('login_ip',$rel[0]['login_ip']);
				session('member_type',$memberType[0]['type_name']);
				session('member_avatar',empty($rel[0]['head_icon']) ? '__PUBLIC__/Images/tx.jpg':'__PUBLIC__/Uploads/'.$rel[0]['head_icon']);
			
				$update['login_date'] = date('Y-m-d H:i:s');
				$update['login_ip'] = get_client_ip();
				$memberMO->where("id=$id")->save($update);
				return;
			}
		}
		$this->display();
	}
	
	/** 
	 * 退出
	 */
	public function logout(){
		session('member_id',null);
		session('member_name',null);
		session('login_date',null);
		session('login_ip',null);
		session('member_type',null);
		$this->display('login');
	}
	/**
	 * 注册
	 */
	public function register(){
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			$memberMO = M('Member');
			$data['member_name'] = strtolower(trim($_POST['username']));
			$data['member_pwd'] = htmlspecialchars(trim($_POST['pwd']));
			$confpwd = trim($_POST['cofpwd']);
			$data['email'] = strtolower(trim($_POST['email']));
			$data['member_type'] = intval($_POST['member_type']);
			$data['register_date'] = date('Y-m-d H:i:s');
			//$data['mobile'] = '123456';
			//$data['telphone'] = '3222809';
			//$data['address'] = '天河';
			//$data['use_money'] = 10000;
			//$data['frozen_money'] = 0;
			//$data['accumulation'] = 10;
			//$data['member_type'] = 1;
			//$data['qq'] = '284512180';
			//$data['gender'] = '1';
			//$data['birthday'] = '1986-07-13';
			$checkcode = trim($_POST['verifycode']);

			$rel = $this->check_member($data);
			if($rel != '')
				exit($rel);
			
			if($data['member_pwd'] != $confpwd){
				exit('两次密码不一样');
			}
			
			if(session('verify') != md5($checkcode)){
				exit('验证码不正确');
			}
			
			if(intval($_POST['clause']) != 1){
				exit('请确认阅读服务协议');
			}
			
			if($memberMO->add($data)){
				session('verify',null);
				return;
			}else{
				exit('注册失败，请联系管理员。');
			}
		}
		$memberTypeMO = M('MemberType');
		$rel = $memberTypeMO->select();
		$this->assign('typeData',$rel);
		$this->display('zhuce');
	}
	
	/**
	 * 增加会员属性
	 */
	public function add_property(){
		$propertyMO = M('MemberProperty');
		$propertyMO->property_name = '电商';
		$propertyMO->property_desc = '电商xx';
		$propertyMO->add();
	}
	/**
	 * 绑定会员属性
	 */
	public function bind_property(){
		$PropertyBindMO = M('PropertyBind');
		$PropertyBindMO->member_id = '2';
		$PropertyBindMO->property_id = '2';
		$PropertyBindMO->add();
	}
	
	/**
	 * 编辑会员信息
	 */
	public function bjzl(){
		$this->isLogin();
		$id = session('member_id');
		$memberMO = M('Member');
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			$data['mobile'] = trim($_POST['mobile']);
			$data['telphone'] = trim($_POST['telphone']);
			$data['qq'] = trim($_POST['qq']);
			$data['gender'] = intval($_POST['gender']);
			$data['birthday'] = $_POST['birthday'];
			$data['address'] = htmlspecialchars($_POST['address']);
			$data['postcode'] = $_POST['postcode'];

			$rel = $this->check_member($data);
			if($rel != '')
				exit($rel);
			
			$memberMO->where("id=$id")->save($data);
			return;
		}
		
		$rel = $memberMO->where("id=$id")->select();
		$this->assign('member',$rel[0]);
		$this->display();
	}
	
	/**
	 * 发布信息
	 */
	public function fbxx(){
		$this->display();
		
	}
	/**
	 * 忘记密码
	 */
	public function forget_kw(){
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			$search['member_name'] = strtolower(trim($_POST['username']));
			$search['email'] = strtolower(trim($_POST['email']));
			$memberMO = M('Member');
			$rel = $memberMO->where($search)->select();
			if(count($rel) < 1){
				exit('请确保你的帐号和邮箱输入正确！');
			}
			$id = $rel[0]['id'];
			$data['member_pwd'] = strtolower(PublicAction::randStr(8));
			
			if($memberMO->where("id=$id")->save($data)){
				$html = $search['member_name'].'你好，你的密码已变更为：<br/><b>'.$data['member_pwd'].'</b>';
				$subject = '家会网会员密码变更';
				$send = PublicAction::send_mail($search['email'], $search['member_name'], $subject, $html);
				if($send !== true){
					exit('发送邮箱失败，请稍候重试 ！'.$send);
				}
			}else{
				exit('密码更改失败，请稍候重试！');
			}
			exit;
		}
		$this->display();
	}
	/**
	 * 家会网络服务使用协
	 */
	public function fuwuxieyi(){
		$this->display();
	}
	
	public function fuwuxieyi2(){
		$this->display();
	}
	/**
	 * 最近发布信息
	 */
	public function gqxx(){
		$this->display();
	}
	/**
	 * 修改密码
	 */
	public function xgkw(){
		$this->isLogin();
		
		if($_SERVER['REQUEST_METHOD'] == 'POST'){
			$id = session('member_id');
			$memberMO = M('Member');
			$oldpwd = htmlspecialchars(trim($_POST['oldpwd']));
			$data['member_pwd'] = htmlspecialchars(trim($_POST['newpwd']));
			$confpwd = htmlspecialchars(trim($_POST['cofpwd']));
			
			if(strlen($data['member_pwd']) < 6 || strlen($data['member_pwd']) > 16){
				exit('不是有效的密码，请输入6-16位字符（字母、数字、符号），区分大小写');
			}
			
			if($oldpwd == $data['member_pwd']){
				exit('原密码和新密码是一样');
			}
			
			if($data['member_pwd'] != $confpwd){
				exit('两次密码不一样');
			}
			
			$rel = $memberMO->where("id=$id")->select();
			if($oldpwd != $rel[0]['member_pwd']){
				exit('原密码不正确');
			}
			
			$memberMO->where("id=$id")->save($data);
			return;
		}
		$this->display();
	}
	/**
	 * 系统信息
	 */
	public function xxgl(){
		$this->isLogin();
		$this->display();
	}
	/**
	 * 注册成功
	 */
	public function zhuce_success(){
		$this->display();
	}
	
	/**
	 * 
	 * 检测用户名
	 */
	public function check_member_name(){
		$memberMO = M('Member');
		$data['member_name'] = strtolower(trim($_POST['username']));
		if(!Verify::isNames($data['member_name'],3,16,'EN')){
				exit($data['member_name'].'不是有效的用户名');
			}else{
				$rel = $this->isExistName($data['member_name']);
				if($rel){
					exit($data['member_name'].'已经注册');
				}
		}
		exit;
	}
	
	/**
	 * 检测email
	 */
	public function check_email(){
		$memberMO = M('Member');
		$data['email'] = strtolower(trim($_POST['email']));
		if(!Verify::isEmail($data['email'])){
			exit($data['email'].'不是有效的邮箱');
		}else{
		$rel = $this->isExistEmail($data['email']);
			if($rel){
				exit($data['email'].'已经注册');
			}
		}
		exit;
	}
	
	/**
	 * 检测密码
	 */
	public function check_password(){
		$memberMO = M('Member');
		$data['member_pwd'] = htmlspecialchars(trim($_POST['pwd']));
		if(strlen($data['member_pwd']) < 6 || strlen($data['member_pwd']) > 16){
			exit($data['member_pwd'].'不是有效的密码');
		}
		exit;
	}
	
	/**
	 * 检测会员信息
	 */
	public function check_member($data){
		//正则类
		
		$memberMO = M('Member');
		
		if(isset($data['member_name'])){
		   	if(!Verify::isNames($data['member_name'],3,16,'EN')){
				return $data['member_name'].'不是有效的用户名';
			}else{
				$rel = $this->isExistName($data['member_name']);
				if($rel){
					return($data['member_name'].'已经注册');
				}
			}
		}
		
		if(isset($data['email'])){
			if(!Verify::isEmail($data['email'])){
				return($data['email'].'不是有效的邮箱');
			}else{
				$rel = $this->isExistEmail($data['email']);
				if($rel){
					return($data['email'].'已经注册');
				}
			}
		}
		
		if(isset($data['member_type'])){
			if($data['member_type'] == 0){
				return('请选择你的注册类型');
			}
		}
		
		if(isset($data['member_pwd'])){
			if(strlen($data['member_pwd']) < 6 || strlen($data['member_pwd']) > 16){
				return($data['member_pwd'].'不是有效的密码');
			}
		}
	
  		if(!empty($data['mobile']) && !Verify::isMobile($data['mobile'])){
			return($data['mobile'].'不是一个有效的手机号码');
		}
		
		if(!empty($data['telphone']) && !Verify::isTelephone($data['telphone'])){
			return($data['telphone'].'不是一个有效的电话号码');
		}
		
		if(!empty($data['qq']) && !is_numeric($data['qq'])){
			return($data['qq'].'不是一个有效的QQ号码');
		}
		
		list($year,$month,$day)=explode("-",$data['birthday']); 
		if(!empty($data['birthday']) && !checkdate(intval($month), intval($day), intval($year))){
			return($data['birthday'].'不是一个有效的出生日期');
		}
		
		if(!empty($data['postcode']) && !Verify::isPostcode($data['postcode'])){
			return($data['postcode'].'不是一个有效的邮编');
		}
		
		return '';
	}
	
	/**
	 * 上传头像
	 */
	public function avatar(){
		$this->isLogin();
//		if($_SERVER['REQUEST_METHOD'] == 'POST'){
//			import('ORG.Net.UploadFile');
//		    $upload = new UploadFile();// 实例化上传类
//		    $upload->thumb = true;
//		    $upload->thumbMaxWidth = '74';
//		    $upload->thumbMaxHeight = '74';
//		    $upload->thumbPrefix = 't_';
//		    $upload->maxSize  = 1048576 ;// 设置附件上传大小
//		    $upload->allowExts  = array('jpg', 'gif', 'png');// 设置附件上传类型
//		    $upload->savePath =  './Public/Uploads/';// 设置附件上传目录
//		    if(!$upload->upload()) {// 上传错误提示错误信息
//		       $this->error($upload->getErrorMsg());
//		  	 }else{// 上传成功
//		        $rel = $upload->getUploadFileInfo();
//		        $memberMO = M('Member');
//		        $data['head_icon'] = $rel[0]['savename'];
//		        $id = session('member_id');
//		        $memberMO->where("id=$id")->save($data);
//		        
//		        session('member_avatar','__PUBLIC__/Uploads/t_'.$data['head_icon']);
//		        $this->success("上传成功");
//		        return;
//		  	 }
//		}
		$this->assign('postUrl',__APP__.'/Member/upload_avatar?action=uploadavatar');
		$this->display();
	}
	
	
	public function upload_avatar(){
		$rs = array();
		switch($_GET['action']){
		
			//上传临时图片
			case 'uploadtmp':
				$file = 'uploadtmp.jpg';
				@move_uploaded_file($_FILES['Filedata']['tmp_name'], $file);
				$rs['status'] = 1;
				$rs['url'] = './php/' . $file;
			break;
		
			//上传切头像
			case 'uploadavatar':
				$input = file_get_contents('php://input');
				$data = explode('--------------------', $input);
				$fileName1 = session('member_name').'_avatar1.jpg';
				//$fileName2 = session('member_name').'_avatar2.jpg';
				@file_put_contents('./Public/Uploads/'.$fileName1, $data[0]);
				//@file_put_contents('./Public/Uploads/'.$fileName2, $data[1]);
				$rs['status'] = 1;
			break;
		
			default:
				$rs['status'] = -1;
		}
		if($rs['status'] == 1){
			$memberMO = M('Member');
			$data['head_icon'] = $fileName1;
			$id = session('member_id');
			$memberMO->where("id=$id")->save($data);
			session('member_avatar','__PUBLIC__/Uploads/'.$fileName1);
		}
		echo json_encode($rs);
	}
}
?>